import React from 'react';
import { Button, Modal } from 'antd';
import List from 'pages/serviceStandard/list.jsx'
import StateMixin from 'reflux-state-mixin'
import StandardAction from 'pages/serviceStandard/actions/standard'
import StandardStore from 'pages/serviceStandard/stores/standard'
import StandardForm from 'pages/serviceStandard/standardForm.jsx'
import StandardFormEdit from 'pages/serviceStandard/standardFormEdit.jsx'
export default React.createClass({
  mixins: [StateMixin.connect(StandardStore)], 
  getInitialState() {
    return {
    }
  },
  modify(id) {
    StandardAction.findStandardById({id: id})
  },
  delete(id) {
    Modal.confirm({
      title: '你是否确定要删除该服务标准',
      onOk() {
        let arr = [];
        arr.push(id)
        StandardAction.delStandard({ids: arr}).then(() => {
          StandardAction.getStandardList();
        })
      },
      onCancel() {},
    });
  },
  showModal() {
    this.setState({
      visible: true
    })
  },
  handleCancel() {
    this.setState({
      visible: false
    })
  },
  ticketSubmit() {
    let formData = this.refs.standardforms.getFieldsValue();
    StandardAction.standardCreate(formData).then(() => {
      this.refs.standardforms.resetFields();
      StandardAction.getStandardList();
    })
  },
  ticketSave() {
    let formData = this.refs.standardformsEdit.getFieldsValue();
    formData.id = this.state.model.id;
    StandardAction.updateStandard(formData).then(() => { 
      this.refs.standardformsEdit.resetFields();
      StandardAction.getStandardList();
    })
  },
  componentDidMount() {
    StandardAction.getStandardList();
  },
  render() {
    return (
      <div className="config-main">
        <div>
          <div className="model_top">
            <div style={{ float: "right" }}>
              <Button type="primary" onClick={this.showModal}>
                <i className="iconfont icon-tianjiaxiao" />新建服务标准
              </Button>
            </div>
          </div>
        </div>
        <div>
          <List modify={this.modify} delete={this.delete} models={this.state.models}/>
        </div>
        <Modal
          title="新建服务标准"
          width={640}
          visible= { this.state.visible }
          onCancel= { 
            () => {
              this.setState({ visible: false })
              setTimeout(() => {
                this.refs.standardforms.resetFields();
              }, 100)
            }
          }
          footer={[]}
        >
          <div className="detail-form">
            <StandardForm
              ref="standardforms"
            />
            <div className="ant-row ant-form-item">
              <div className="ant-col-offset-3 ant-form-item-label">
                <Button type="primary" onClick={this.ticketSubmit}>
                  创建  
                </Button>
              </div>
            </div>
          </div>
        </Modal>
        <Modal
          title="修改服务标准"
          width={640}
          visible= { this.state.visible2 }
          onCancel= { 
            () => {
              this.setState({ 
                visible2: false 
              })
              this.refs.standardformsEdit.resetFields();
            } 
          }
          footer={[]}
        >
          <div className="detail-form">
            <StandardFormEdit
              ref="standardformsEdit"
              model={this.state.model}
            />
            <div className="ant-row ant-form-item">
              <div className="ant-col-offset-3 ant-form-item-label">
                <Button type="primary" onClick={this.ticketSave}>
                  保存  
                </Button>
              </div>
            </div>
          </div>
        </Modal>
      </div>
    )
  }
})
